
from flask import Blueprint, render_template, redirect, url_for, session, flash, jsonify, g
from flask import request
from flask_paginate import get_page_parameter, Pagination
from werkzeug.utils import secure_filename
from exts import db, mail
import string
import random
from models import UserModel,ProductModel
from werkzeug.security import generate_password_hash, check_password_hash
from decorators import login_req, login_required, admin_required

bp = Blueprint("products", __name__, url_prefix="/products")

@bp.route("/",methods=['GET'])
@login_required
def products():
    products = ProductModel.query.all()
    result = []
    for product in products:
        product_data = {
            'id': product.id,
            'name': product.name,
            'barcode': product.barcode,
            'price': float(product.price),
            'stock_quantity': product.stock_quantity,
            'supplier': product.supplier
        }
        result.append(product_data)
    return render_template("商品管理.html",products=result)

@bp.route("/manage", methods=['POST'])
@login_required
def manage_product():
    product_id = request.form['product_id']
    new_product_name = request.form['new_product_name']
    new_barcode = request.form['new_barcode']
    new_price = float(request.form['new_price'])
    new_stock_quantity = int(request.form['new_stock_quantity'])
    new_supplier = request.form['new_supplier']

    product = ProductModel.query.get(product_id)
    if product:
        product.name = new_product_name
        product.barcode = new_barcode
        product.price = new_price
        product.stock_quantity = new_stock_quantity
        product.supplier = new_supplier
        db.session.commit()
        flash('商品信息更新成功', 'success')
    else:
        flash('商品不存在', 'error')

    return redirect(url_for('products.products'))

@bp.route("/delete", methods=['POST'])
@login_required
def delete_product():
    product_id = request.form['product_id']

    product = ProductModel.query.get(product_id)
    if product:
        db.session.delete(product)
        db.session.commit()
        flash('商品删除成功', 'success')
    else:
        flash('商品不存在', 'error')

    return redirect(url_for('products.products'))

@bp.route('/add_product', methods=['POST'])
@admin_required
@login_required
def add_product():
    name = request.form['product_name']
    barcode = request.form['barcode']
    price = request.form['price']
    stock_quantity = request.form['stock_quantity']
    supplier = request.form['supplier']

    product = ProductModel(name=name, barcode=barcode, price=price, stock_quantity=stock_quantity, supplier=supplier)
    db.session.add(product)
    db.session.commit()

    return redirect(url_for('products.products'))


@bp.route('/search_products', methods=['GET'])
@login_required
def search_products():
    search_text = request.args.get('search')  # 获取查询字符串中的搜索关键字
    if search_text:
        products = ProductModel.query.filter(
            (ProductModel.name.ilike(f"%{search_text}%")) |
            (ProductModel.supplier.ilike(f"%{search_text}%"))
        ).all()
    else:
        products = ProductModel.query.all()

    result = []
    for product in products:
        product_data = {
            'id': product.id,
            'name': product.name,
            'barcode': product.barcode,
            'price': float(product.price),
            'stock_quantity': product.stock_quantity,
            'supplier': product.supplier
        }
        result.append(product_data)
    return render_template("商品管理.html", products=result)